package com.gaohe.Listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class countLis implements HttpSessionListener,HttpSessionAttributeListener{
    public static int count = 0;
//    有两种退出登录的方法，一种是直接关闭浏览器，session消失；第二种方法是点击用户名退出，logout调用方法使session中参数消失
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
//        session创建并且参数添加进去后，表示登录成功，要count++
        if (httpSessionBindingEvent.getName().equals("manager")){
            count++;
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
//        点击用户名主动退出时，移除参数,count--
        if (httpSessionBindingEvent.getName().equals("manager")){
            count--;
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//        直接关闭浏览器进行退出,session会被销毁
        if (httpSessionEvent.getSession().getAttribute("manager").equals(null)){
            count--;
        }
    }
}
